package com.college.common.utils.string;

import cn.hutool.core.text.CharSequenceUtil;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * @author 小C工作室
 * @ClassName: StringUtil
 * @Description: 字符串处理类
 * @date 2015-12-24 下午2:15:03
 */
public class StringUtil {
	public static final String CURRENCY_FEN_REGEX = "\\-?[0-9]+";

	/**
	 * @param @param string
	 * @return boolean
	 * @throws
	 * @Title: isEmpty
	 * @Description: 判断字符串是否为空
	 */
	public static boolean isEmpty(String string) {
		boolean result = false;
		if (string == null || "".equals(string.trim()) || "null".equals(string)) {
			result = true;
		}
		return result;
	}

	/**
	 * 根据身份证获取性别
	 *
	 * @param cardNo
	 * @return
	 */
	public static String getSex(String cardNo) {
		String sexStr = "0";
		if (cardNo.length() == 15) {
			sexStr = cardNo.substring(14, 15);
		} else if (cardNo.length() == 18) {
			sexStr = cardNo.substring(16, 17);
		}
		int sexNo = Integer.parseInt(sexStr);
		return sexNo % 2 == 0 ? "女" : "男";
	}


	/**
	 * 验证Object是否为空,object instanceof String
	 *
	 * @param object
	 * @return
	 */
	public static boolean isEmpty(Object object) {
		boolean result = false;
		if (object == null || "".equals(object.toString().trim())) {
			result = true;
		}
		return result;
	}

	/**
	 * @return String 返回类型
	 * @throws
	 * @Title: getUuid
	 * @Description: 获取UUID 带-标识
	 */
	public static String getUuid() {
		UUID uuid = UUID.randomUUID();
		String str = uuid.toString();
		return str;
	}

	/**
	 * @return
	 * @Title: getUUID
	 * @Description: 获取UUID 去掉-标识
	 */
	public static String getUUID() {
		String str = getUuid();
		str = str.replace("-", "");
		return str;
	}

	/**
	 * 获取随机数
	 *
	 * @param len 随机数长度
	 * @return 返回len长度的随机数
	 */
	public static StringBuffer getRandomCode(int len) {
		StringBuffer buffer = new StringBuffer();
		Random random = new Random();
		for (int i = 0; i < len; i++) {
			buffer.append(random.nextInt(10));
		}
		return buffer;
	}

	/**
	 * 将字符串为"null"或空对象转化为字符串""
	 *
	 * @param obj
	 */
	public static String doNullStr(Object obj) {
		String str = "";
		if (obj != null) {
			str = String.valueOf(obj);
			if (str.equals("null")) {
				str = "";
			}
		}
		return str.trim();
	}

	public static String doNullStr0(Object obj) {
		String str = "0";
		if (obj != null) {
			str = String.valueOf(obj);
			if (str.equals("null")) {
				str = "0";
			}
		}
		return str.trim();
	}

	public static String changeF2YStr(String amount) throws Exception {
		if (!amount.matches(CURRENCY_FEN_REGEX)) {
			throw new Exception("金额格式有误");
		}
		return BigDecimal.valueOf(Long.valueOf(amount)).divide(new BigDecimal(100)).toString();
	}

	public static BigDecimal changeF2Y(String amount) throws Exception {
		if (!amount.matches(CURRENCY_FEN_REGEX)) {
			throw new Exception("金额格式有误");
		}
		return BigDecimal.valueOf(Long.valueOf(amount)).divide(new BigDecimal(100));
	}

	/**
	 * @param @param  str
	 * @param @param  code
	 * @param @return
	 * @param @throws UnsupportedEncodingException 参数说明
	 * @return String 返回类型
	 * @throws
	 * @Title: encode
	 * @Description: 根据指定编码对字符串进行转码
	 */
	public static String encode(String str, String code) throws UnsupportedEncodingException {
		if (isEmpty(str)) {
			return "";
		}
		return java.net.URLEncoder.encode(str, code);
	}

	/**
	 * @param @param  str
	 * @param @return
	 * @param @throws UnsupportedEncodingException 参数说明
	 * @return String 返回类型
	 * @throws
	 * @Title: encode
	 * @Description: 对字符转进行UTF-8转码
	 */
	public static String encode(String str) throws UnsupportedEncodingException {
		if (isEmpty(str)) {
			return "";
		}
		return encode(str, "UTF-8");
	}

	/**
	 * @param @param  str
	 * @param @param  code
	 * @param @return
	 * @param @throws UnsupportedEncodingException 参数说明
	 * @return String 返回类型
	 * @throws
	 * @Title: decode
	 * @Description: 根据指定编码对字符串进行解码
	 */
	public static String decode(String str, String code) throws UnsupportedEncodingException {
		if (isEmpty(str)) {
			return "";
		}
		return java.net.URLDecoder.decode(str, code);
	}

	/**
	 * @param @param  str
	 * @param @return
	 * @param @throws UnsupportedEncodingException 参数说明
	 * @return String 返回类型
	 * @throws
	 * @Title: decode
	 * @Description: 对字符转进行UTF-8解码
	 */
	public static String decode(String str) throws UnsupportedEncodingException {
		if (isEmpty(str)) {
			return "";
		}
		return decode(str, "UTF-8");
	}

	/**
	 * 获取堆栈信息
	 * @param throwable
	 * @return
	 */
	public static String getStackTrace(Throwable throwable) {
		StringWriter sw = new StringWriter();
		try (PrintWriter pw = new PrintWriter(sw)) {
			throwable.printStackTrace(pw);
			return sw.toString();
		}
	}


	/**
	 * 获取调用者
	 *
	 * @return
	 */
	public static String getInvokeName() {
		StackTraceElement stackTraceElement = new Throwable().getStackTrace()[2];
		return CharSequenceUtil.format("{}:{}:{}", stackTraceElement.getClassName(), stackTraceElement.getMethodName(), stackTraceElement.getLineNumber());
	}

	public static List<String> getLatelyInvokeNames() {
		ArrayList<String> list = new ArrayList<>();
		StackTraceElement[] stackTraceElement = new Throwable().getStackTrace();
		for (StackTraceElement traceElement : stackTraceElement) {
			String className = traceElement.getClassName();
			int lineNumber = traceElement.getLineNumber();
			if (className.contains("com.college") && lineNumber > 0) {
				list.add(CharSequenceUtil.format("【{}】【{}】【{}】", traceElement.getClassName(), traceElement.getMethodName(), lineNumber));
			}
		}
		list.remove(0);
		return list;
	}
}
